package com.cskaoyan.controller.front.coupon;


import com.cskaoyan.bean.bo.front.coupon.SelectCouponUserBo;
import com.cskaoyan.bean.po.BaseParam;
import com.cskaoyan.bean.po.MarketCoupon;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.admin.BaseDataVo;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.front.coupon.WxCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * wx优惠券控制器
 *
 * @author 李然
 * @date 2022/10/14
 *
 *
 * /**
 *  * coupon/receive: post
 *  *                          {"errno":742,"errmsg":"优惠券不正确"
 *  *
 *  * coupon/exchange: post
 *  *                          {"errno":740,"errmsg":"优惠券已经领取过"}
 *  *
 *  * coupon/list: get
 *  *                          "id": 122,
 *  *                          "name": "1",
 *  *                          "desc": "1",
 *  *                          "tag": "1",
 *  *                          "discount": 0.00,
 *  *                          "min": 0.00,
 *  *                          "days": 0
 *  *
 *  * coupon/mylist: get
 *  *                          "id": 17389,
 *  *                          "cid": 88,
 *  *                          "name": "aa",
 *  *                          "desc": "vv",
 *  *                          "tag": "bb",
 *  *                          "min": 199.00,
 *  *                          "discount": 20.00,
 *  *                          "startTime": "2022-10-14 17:53:42",
 *  *                          "endTime": "2022-10-21 17:53:42",
 *  *                          "available": false
 *  *
 *  * coupon/selectlist: get
 *  *                          "id": 7469,
 *  *                          "cid": 77,
 *  *                          "name": "ssssssssss",
 *  *                          "desc": "sssssssss",
 *  *                          "tag": "ssssssss",
 *  *                          "min": 33.00,
 *  *                          "discount": 33.00,
 *  *                          "startTime": "2022-09-12 09:52:14",
 *  *                          "endTime": "2022-10-15 09:52:14",
 *  *                          "available": false
 *  * */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService wxCouponService;

    @GetMapping("mylist")
    public BaseRespVo getMyCouponList(SelectCouponUserBo couponBo) {

        BaseDataVo couponUserList = wxCouponService.getMyCouponUserList(couponBo);
        return BaseRespVo.ok(couponUserList);
    }

    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map) {
        String code = (String) map.get("code");
        int i = wxCouponService.exchange(code);

        if(i == 742){
            return BaseRespVo.couponError(i,"优惠券不正确");
        }

        return BaseRespVo.ok();
    }


    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map map) throws MarketValidationException {
        Integer couponId = (Integer) map.get("couponId");
        BaseRespVo baseRespVo = wxCouponService.receiveCoupon(couponId);
//
//        if (i == 740) {
//            return BaseRespVo.couponError(i,"优惠券已领完");
//        }
//        return 0;
        return baseRespVo;
    }

    @GetMapping("list")
    public BaseRespVo getCouponList(BaseParam param) {
        BaseDataVo<MarketCoupon> couponList = wxCouponService.getWxCouponList(param);
        return BaseRespVo.ok(couponList);
    }

    @GetMapping("selectlist")
    public BaseRespVo selectCouponlist(Integer cartId) {

        BaseDataVo baseDataVo = wxCouponService.selectCouponList(cartId);

        return BaseRespVo.ok(baseDataVo);
    }
}
